#include "music_lyricparser.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QRegExp>

music_lyricparser::music_lyricparser()
{
     m_lrcList.clear();
}

void music_lyricparser::setJson(const QString &jsonStr)
{
    m_lrcList.clear();

    QJsonParseError err;
    QJsonDocument doc = QJsonDocument::fromJson(jsonStr.toUtf8(), &err);
    if (err.error != QJsonParseError::NoError || !doc.isObject()) return;

    QJsonObject obj = doc.object();
    if (!obj.contains("lrc")) return;

    QString rawLyric = obj["lrc"].toObject().value("lyric").toString();

    QStringList lines = rawLyric.split('\n', QString::SkipEmptyParts);
    QRegExp timeExp("\\[(\\d{2}):(\\d{2})\\.(\\d{2})\\]");

    for (const QString &line : lines) {
        QString content = line;
        while (timeExp.indexIn(content) != -1) {
            int min = timeExp.cap(1).toInt();
            int sec = timeExp.cap(2).toInt();
            int msec = timeExp.cap(3).toInt() * 10;
            qint64 totalMs = min * 60000 + sec * 1000 + msec;

            content.remove(0, timeExp.matchedLength());
            QString text = content.trimmed();
            if (!text.isEmpty()) {
                m_lrcList.append({totalMs, text});
            }
        }
    }
}

QList<LrcLine> music_lyricparser::getLrcList() const
{
    return m_lrcList;
}

QStringList music_lyricparser::getPureLyrics() const
{
    QStringList list;
    for (const LrcLine &line : m_lrcList)
        list << line.content;
    return list;
}




